﻿using Common.Contract;
using Common.Contract.Authentication.Record;
using Common.Contract.Customer;
using Common.Contract.Customer.Request;
using Common.Contract.Customer.Response;
using Common.Contract.Customer.Response.CustomerManager;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service;
using Customer360AutoJob.Core.Configuration;
using Customer360AutoJob.Jobs;
using Customer360AutoJob.Models.ViewModels.ExportDataToSmartSME;
using Quartz;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using System.Web;

namespace Customer360AutoJob.Jobs
{
    public class ExportAlertMaturity : JobBase, IJob
    {
        #region Properties
        public UserRecord CurrentUserRecord { get; set; }

        #endregion
        public void Execute(IJobExecutionContext context)
        {
            CurrentUserRecord = new UserRecord
            {
                UserName = "admin",
                GroupIdentifier = "1"
            };
            var listCustomer = RetrieveLoanMaturity();
            DataTable dt = new DataTable();
            dt = ToDataTable(listCustomer);
            var fileName = "TableLoanMaturity" + DateTime.Now.ToString("dd/MM/yyyy") + ".xlsx";

            fileName = fileName.Replace("/", "");
            ExportToExcel(dt, fileName);
            Thread.Sleep(100);

            var listDepositMatdt = RetrieveListDepositMaturity();
            dt = ToDataTable(listDepositMatdt);
            fileName = "TableDepositMaturity" + DateTime.Now.ToString("dd/MM/yyyy") + ".xlsx";
            fileName = fileName.Replace("/", "");
            ExportToExcel(dt, fileName);
            Thread.Sleep(100);

            var listLimitMatdt = RetrieveListLimitMaturity();
            dt = ToDataTable(listLimitMatdt);
            fileName = "TableCustomerLimitMaturityMBD" + DateTime.Now.ToString("dd/MM/yyyy") + ".xlsx";
            fileName = fileName.Replace("/", "");
            ExportToExcel(dt, fileName);
            Thread.Sleep(100);
        }

        private List<MaturityLimit> RetrieveListLimitMaturity()
        {
            List<MaturityLimit> result = new List<MaturityLimit>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                FilterRecord filter = new FilterRecord();
                filter.IsExport = true;
                RetrieveCustomerAlertRequest request = new RetrieveCustomerAlertRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerAlertController.AlertLimitMatdt"
                    },
                    Page = new PageRecord
                    {
                        PageSize = 0,
                        PageIndex = 0
                    },
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = new RetrieveCustomerAlertLimitResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerAlertLimit(request);
                result = (customerRecords == null || customerRecords.Records == null) ? new List<MaturityLimit>() : customerRecords.Records.Select(x => new MaturityLimit
                {
                    BUSINESS_DATE = x.BusinessDate.Value,
                    CIF = x.Cif,
                    ID = x.Id,
                    APPROVAL_DATE = x.ApprovalDate.Value,
                    EXPIRY_DATE = x.ExpiryDate.Value,
                    INTERNAL_AMOUNT_QD = x.InternalAmountQd
                }).ToList();
            }
            return result;
        }

        private List<MaturityDeposit> RetrieveListDepositMaturity()
        {
            List<MaturityDeposit> result = new List<MaturityDeposit>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                FilterRecord filter = new FilterRecord();
                filter.IsExport = true;
                RetrieveCustomerAlertRequest request = new RetrieveCustomerAlertRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerAlertController.AlertDepositMatdt"
                    },
                    Page = new PageRecord
                    {
                        PageSize = 0,
                        PageIndex = 0
                    },
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = new RetrieveTblAlertDepositMatdtResponse();
                customerRecords = serviceClient.Proxy.RetrieveTblAlertDepositMatdt(request);
                result = (customerRecords == null || customerRecords.Records == null) ? new List<MaturityDeposit>() : customerRecords.Records.Select(x => new MaturityDeposit
                {
                    CIF = x.Cif,
                    ACCTNO = x.Acctno,
                    TERM = x.Term,
                    OPENNING_DATE = x.OpenningDate,
                    MATDT = x.Matdt,
                    BAL_QD = x.BalQd
                }).ToList();
            }
            return result;
        }

        private List<MaturityLoan> RetrieveLoanMaturity()
        {
            List<MaturityLoan> result = new List<MaturityLoan>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                FilterRecord filter = new FilterRecord();
                filter.IsExport = true;
                RetrieveCustomerAlertRequest request = new RetrieveCustomerAlertRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerAlertController.AlertLoanMatdt"
                    },
                    Page = new PageRecord
                    {
                        PageSize = 0,
                        PageIndex = 0
                    },
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = new RetrieveCustomerLoanMatdtForExportResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerLoanMatdtForExport(request);
                result = (customerRecords == null || customerRecords.Records == null) ? new List<MaturityLoan>() : customerRecords.Records.Select(x => new MaturityLoan
                {
                    ID = x.Id,
                    BUSINESS_DATE = x.BusinessDate,
                    CIF = x.Cif,
                    ACCTNO = x.Acctno,
                    CCY = x.CCY,
                    TERM = x.Term,
                    ISSUE_DATE = x.IssueDate,
                    OPENNING_DATE = x.OpenningDate,
                    MATDT = x.Matdt,
                    RATE = x.Rate,
                    NHOM_SP_LOAN = x.NhomSpLoan,
                    BAL = x.Bal,
                    BAL_QD = x.BalQd
                }).ToList();
            }
            return result;
        }

    }
}